package com.baibei.module;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;

import com.baibei.basic.BaibeiBasicActivity;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.model.UserInfo;
import com.baibei.model.event.LoginEvent;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;
import com.baibei.sdk.RxObservable;
import com.baibei.ui.AppUI;
import com.rae.swift.Rx;
import com.rae.swift.session.SessionManager;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Activity基类
 * Created by ChenRui on 2017/8/1 0001 16:47.
 */
public abstract class BasicActivity extends BaibeiBasicActivity implements UserStatus{

    private Unbinder mUnBinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        }

        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID, boolean isBind) {
        super.setContentView(layoutResID, isBind);
        if (isBind) {
            mUnBinder = ButterKnife.bind(this);
        }

    }

    @Override
    protected void onDestroy() {
        //销毁前，如果有窗口未关闭，先关闭
        AppUI.dismiss();
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
            mUnBinder = null;
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (Rx.isEmpty(fragments)) return;
        for (Fragment fragment : fragments) {
            // 通知里面的Fragment
            if (fragment != null) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    @Override
    protected void toast(String msg) {
//        AppUI.toast(this, msg);
    }

    public void showHomeAsUp(boolean enable) {
        if (enable) {
            showHomeAsUp();
        } else {
            ActionBar bar = getSupportActionBar();
            if (bar != null) {
                bar.setDisplayShowHomeEnabled(false);
                bar.setDisplayHomeAsUpEnabled(false);
            }
        }
    }

    public void logout() {
        // 调用退出接口
        RxObservable.create(ApiFactory.getInstance().getUserApi().signOut(), "signOut")
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        AppUI.loading(getContext(), "请稍后...");
                    }
                })
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        AppUI.dismiss();
                        // 1、清除登录信息
                        SessionManager.getDefault().clear();

                        // 2、发送退出事件
                        LoginEvent event = new LoginEvent();
                        event.setLogout(true);
                        EventBus.getDefault().post(event);

                        // 3、清除所有界面跳转首页
//                        BasicActivity.this.finish();
//                        AppRouter.finishActivities();
                        AppRouter.routeToMain(getContext());
                    }
                })
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String s) {
                    }

                    @Override
                    protected void accept(Empty empty) {
                    }
                });
    }

    /**
     * 友盟统计事件
     *
     * @param event event
     */
    protected void onUmengEvent(String event) {
        MobclickAgent.onEvent(this, event);
    }

    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean isLogin() {
        return SessionManager.getDefault().isLogin();
    }

    @Override
    public boolean isRealName() {
        if (isLogin()) {
            UserInfo userInfo = SessionManager.getDefault().getUser();
            if (userInfo == null) {
                return false;
            }
            return userInfo.isPassRealNameVerify();
        }
        return false;
    }
}
